//
//  Fraction.m
//  02-Fraction
//
//  Created by dllo on 16/7/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (id)initWitnMember:(NSInteger)member denominator:(NSInteger)denominator{
    self = [super init];
    if(self != nil){
        self.member = member;
        self.denominator = denominator;
    }
    return self;
}

- (NSInteger)gcdNumber:(NSInteger)member denominator:(NSInteger)denominator{
    NSInteger a = member % denominator;
    while (a != 0) {
        member = denominator;
        denominator = a;
        a = member % denominator;
    }
    return denominator;
}

- (void)simplifyNumber{
    NSInteger divisor = [self gcdNumber:self.member denominator:self.denominator];
    self.member/= divisor;
    self.denominator/= divisor;
}


- (Fraction *)sumNumber:(Fraction *)fraction{
    Fraction *fr = [[Fraction alloc] init];
    fr.member = [self member]*[fraction denominator] + [fraction member]*[self denominator];
    fr.denominator = [fraction denominator]*[self denominator];
    [fr simplifyNumber];
    return fr;
}

- (Fraction *)reduceNumber:(Fraction *)fraction{
    Fraction *fr = [[Fraction alloc] init];
    fr.member = self.member*fraction.denominator - fraction.member*self.denominator;
    fr.denominator = self. denominator*fraction.denominator;
    [fr simplifyNumber];
    return fr;
}

- (Fraction *)rideNumber:(Fraction *)fraction{
    Fraction *fr = [[Fraction alloc] init];
    fr.member = [self member]*[fraction member];
    fr.denominator = [self denominator]*[fraction denominator];
    [fr simplifyNumber];
    return fr;
}

- (Fraction *)divideNumber:(Fraction *)fraction{
    Fraction *fr = [[Fraction alloc] init];
    fr.member = [self member]*[fraction denominator];
    fr.denominator = [self denominator]*[fraction member];
    [fr simplifyNumber];
    return fr;
}

- (NSComparisonResult)resultCompar:(Fraction *)fraction{
    NSInteger sum1 = [self member]*[fraction denominator];
    NSInteger sum2 = [fraction member]*[self denominator];
    NSInteger sum = sum1 - sum2;
    if (sum > 0) {
        return NSOrderedSame;
    }else if (sum == 0){
        return NSOrderedSame;
    }else{
        return NSOrderedAscending;
    }
}
@end
